//
//  SearchResultUserViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/10/12.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "TT_SearchHeadView.h"

#import "SearchResultUserViewController.h"

@interface SearchResultUserViewController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

@property (nonatomic, strong) UserInfoModel *model;

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) TT_SearchHeadView *searchHeadView;

@end

@implementation SearchResultUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {
    
    self.title = @"选择联系人";
    self.view.backgroundColor = UIColorHex(0xffffff);
    
    [self.view addSubview:self.searchHeadView];
    [self.searchHeadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.searchHeadView.mas_bottom);
    }];
    
    [self.tableview reloadData];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if ([Utilities checkPhoneTel:textField.text]) {
        [self request_UserInfo];
    } else {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        [SVProgressHUD dismissWithDelay:1];
    }
    
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.model.username.length > 0) {
        return 1;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SrarchResultUserCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SrarchResultUserCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.model];
    
    WEAKSELF
    cell.SearchResultUserCellBlock = ^{
      
        if (weakSelf.SearchResultUserBlock) {
            weakSelf.SearchResultUserBlock(self.model);
        }
        [weakSelf.navigationController popViewControllerAnimated:YES];
        
    };
    return cell;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = UIColorHex(0xffffff);

        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"SrarchResultUserCell" bundle:nil] forCellReuseIdentifier:@"SrarchResultUserCellID"];
    }
    return _tableview;
}

- (TT_SearchHeadView *)searchHeadView {
    if (!_searchHeadView) {
        _searchHeadView = [[NSBundle mainBundle] loadNibNamed:@"TT_SearchHeadView"owner:nil options:nil].firstObject;
        _searchHeadView.searchTF.placeholder = @"输入联系人手机号";
        _searchHeadView.searchTF.keyboardType = UIKeyboardTypePhonePad;
        _searchHeadView.searchTF.delegate = self;
    }
    return _searchHeadView;
}


- (void)request_UserInfo {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.searchHeadView.searchTF.text forKey:@"username"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_MyInfo];
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];

        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];

                weakSelf.model = [UserInfoModel modelWithDictionary:resultDic];
                
                if (weakSelf.model.username) {
                    
                } else {
                    [[TTNoResultManager shareManager] showNoResultWithStateInView:weakSelf.tableview errorCode:kTTViewState_NoSearchResult btnTitle:@"" didPressButton:^{}];
                }
                
                [weakSelf.tableview reloadData];
            }
        } else {
            [[TTNoResultManager shareManager] showNoResultWithStateInView:weakSelf.tableview errorCode:kTTViewState_NoSearchResult btnTitle:@"" didPressButton:^{}];
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

@end
